<?php
error_reporting(0);//E_ALL
define('SCRIPT_NAME',basename(__FILE__));
define("APP_ROOT",dirname(dirname(__FILE__)));
defined("_D_") || define("_D_",DIRECTORY_SEPARATOR);
defined('IN_QISHI') || define('IN_QISHI',true);
require(APP_ROOT._D_."data"._D_."config.php");
require(APP_ROOT._D_."jkenCaiji"._D_."caiji.php");
require(APP_ROOT._D_.'include'._D_.'common.inc.php');
require(APP_ROOT._D_."include"._D_."mysql.class.php");
header("Content-Type:text/html;charset=utf-8");
$conn = new mysql($dbhost,$dbuser,$dbpass,$dbname,'utf8');
$conn->showError=false;

class jincheng extends caiji{
	public function __construct(){
		parent::__construct();
	}

	public function goinCityPage(){
		$hostName='http://www.jincheng18.com';
		$cityPageArr=array('苏州站'=>'/su/job/su.html','上海站'=>'/sh/job/sh.html','东莞站'=>'/dg/job/dg.html','深圳站'=>'/sz/job/sz.html','广州站'=>'/gz/job/gz.html');
		$categoryInfoFile=APP_ROOT._D_.'jkenCaiji'._D_.'cache'._D_.'listfiles'._D_.'jinchengCategory.txt';
		file_put_contents($categoryInfoFile,'');
		foreach($cityPageArr as $siteName => $siteURI){
			$siteURL=$hostName.$siteURI;
			$this->caijiCore->url=$siteURL;
			$this->caijiCore->pickData();

			$cateInfo=array();
			$cateInfo=$this->caijiCore->parseDataOnce('#<table.*?id="tab2"[^>]+>(.+?)</table>#is');
			if(empty($cateInfo[1])){
				echo "no cateinfo in this site:".$siteURL."<br />\n";
				continue;
			}

			$subReg='#<a href=\'([^\']+)\'[^>]+>#is';
			if(preg_match_all($subReg,$cateInfo[1],$subcatematch)){
				foreach ($subcatematch[1] as $match){
					$this->storeUrlIntoFile($hostName.$match,$categoryInfoFile);
				}
			}
		}//end of foreach

		$this->getListInfo($hostName , $categoryInfoFile);
	}//end of function

	protected function getListInfo($hostName , $categoryInfoFile){
		$phpName=APP_ROOT._D_.'jkenCaiji'._D_.'cache'._D_.'listfiles'._D_.basename(SCRIPT_NAME,'.php').'.txt.php';
		file_put_contents($phpName,'<?'."php\n".'$field=array (\'atime\' => 0,\'pageurl\' => 1);'."\n");
		$urlfileName=APP_ROOT._D_.'jkenCaiji'._D_.'cache'._D_.'listfiles'._D_.basename(SCRIPT_NAME,'.php').'.txt';
		file_put_contents($urlfileName,'');

		$endDataValue=strtotime("-3 days");
		$catePageInfo=file_get_contents($categoryInfoFile);
		$catePageInfo=trim($catePageInfo);
		$cateUrlList=explode("\n",$catePageInfo);
		$totalPage=200;
		foreach($cateUrlList as $pageUrl){
			$forBreak=false;
			$matchCount=0;
			$listTpl=str_replace("pn1","pn{{page}}",$pageUrl);
			for($curPage=1;$curPage<=$totalPage;$curPage++){
				$tmpPage=str_replace('{{page}}',$curPage,$listTpl);
				echo $tmpPage;
				echo "<br />\n";
				$this->caijiCore->url=$tmpPage;
				$this->caijiCore->pickData();
				$tmpData=$this->caijiCore->cutData("id=\"font14\" class=\"data-table\">","<div class=\"line\"",'in');
				preg_match_all('#<a target="_blank" href="([^"]+)"[^>]+>#is',$tmpData,$matchesLink);
				preg_match_all('#<td align="left">\s+([\d\-]+)(&nbsp;)?\s+</td>#is',$tmpData,$matchesDate);

				if(!$matchesLink[1]){
					$matchCount++;
					if($matchCount>1){
						break;
					}
					continue;
				}

				foreach($matchesLink[1] as $index => $match){
					$macthDate=$matchesDate[1][$index];
					$tmpMatchUrl = $hostName.$match;
					$filecontents=$macthDate."\t,\t".$tmpMatchUrl;

					$this->storeUrlIntoFile($filecontents,$urlfileName);
					$tmpdate = strtotime($macthDate);
					if($tmpdate < $endDataValue){
						$forBreak=true;
						break;
					}
				}

				if($forBreak){
					break;
				}
			}//end of page
		}//end of cate list
	}//end of function

	protected function handleDetailData($fieldValue , $tmpData){
		global $conn;

		$cityArr=array('/su/'=>'江苏省 / 苏州市','/sh/'=>'上海市 / 上海市','/dg/'=>'广东省 / 东莞市','/sz/'=>'深圳市 / 深圳市','/gz/'=>'广东省 / 广州市');
		
		$tmpData=$this->caijiCore->cutData('<table cellpadding="0" cellspacing="0">','更多求职者请进入','in');
		if(empty($tmpData)){
			return false;
		}
		$resume=array();
		$resume["uid"]='1';
		$resume["subsite_id"]='0';
		$resume["tpl"]='fengyehanlu';
		$resume["display"]='1';
		$resume["display_name"]='1';
		$resume["audit"]='1';
		$resume["title"]='我的简历';
		$resume["fullname"]='';
		$resume["telephone"]='';
		$resume["email"]='';
		$resume["email_notify"]=0;
		$resume["nameImg"]='';
		$resume["telImg"]='';
		$resume["emailImg"]='';
		
		$tmpCityCn='';
		foreach($cityArr as $citySign => $cityName){
			if(stripos($fieldValue['pageurl'],$citySign) > 0){
				$ciytArr = explode(' / ',$cityName);
				$provinceId = $this->dbGetZone($ciytArr[0]);
				$cityId = $this->dbGetZone($ciytArr[1] , $provinceId);
				
				$resume["district"]=$provinceId;
				$resume["sdistrict"]=$cityId;
				$resume["district_cn"]=$cityName;
				if(strpos($ciytArr[0],'省')){
					$tmpCityCn.=$ciytArr[0];
				}
				$tmpCityCn.=$ciytArr[1];
				break;
			}
		}

		$hostName='http://www.jincheng18.com';
		$imgMatches=false;
		$countMatch=preg_match('#<img align="top" alt="称呼" src="([^"]+)">#is',$tmpData,$imgMatches);
		if($countMatch){
			$imgsrc = $imgMatches[1];
			$tmpImgsrc=$imgsrc;
			$imgsrc=stripos($imgsrc,"http")===0 ? $imgsrc : $hostName.'/'.$imgsrc;

			$basename=md5($tmpImgsrc).'.jpg';

			$subDir=date("Ym");
			$localDir="data"._D_."images"._D_.$subDir;
			$thumbDir="data"._D_."images"._D_."thumb"._D_.$subDir;
			$uriImg = $this->caijiCore->saveImg($imgsrc , $basename , $localDir ,true,$thumbDir);
			if($uriImg){
				$resume["nameImg"]=empty($resume["nameImg"]) ? $subDir.'/'.$basename : $resume["nameImg"];
			}
		}
		$countMatch=preg_match('#<img alt="电话" src="([^"]+)">#is',$tmpData,$imgMatches);
		if($countMatch){
			$imgsrc = $imgMatches[1];
			$tmpImgsrc=$imgsrc;
			$imgsrc=stripos($imgsrc,"http")===0 ? $imgsrc : $hostName.'/'.$imgsrc;

			$basename=md5($tmpImgsrc).'.jpg';

			$subDir=date("Ym");
			$localDir="data"._D_."images"._D_.$subDir;
			$thumbDir="data"._D_."images"._D_."thumb"._D_.$subDir;
			$uriImg = $this->caijiCore->saveImg($imgsrc , $basename , $localDir ,true,$thumbDir);
			if($uriImg){
				$resume["telImg"]=empty($resume["telImg"]) ? $subDir.'/'.$basename : $resume["telImg"];
			}
		}
		$countMatch=preg_match('#<img alt="电子邮箱" src="([^"]+)">#is',$tmpData,$imgMatches);
		if($countMatch){
			$imgsrc = $imgMatches[1];
			$tmpImgsrc=$imgsrc;
			$imgsrc=stripos($imgsrc,"http")===0 ? $imgsrc : $hostName.'/'.$imgsrc;

			$basename=md5($tmpImgsrc).'.jpg';

			$subDir=date("Ym");
			$localDir="data"._D_."images"._D_.$subDir;
			$thumbDir="data"._D_."images"._D_."thumb"._D_.$subDir;
			$uriImg = $this->caijiCore->saveImg($imgsrc , $basename , $localDir ,true,$thumbDir);
			if($uriImg){
				$resume["emailImg"]=empty($resume["emailImg"]) ? $subDir.'/'.$basename : $resume["emailImg"];
			}
		}
		
		//年龄
		$sexList=array('男'=>1,'女'=>2);
		$resume['sex']=1;
		$resume["sex_cn"]='男';
		preg_match('/（(.*?)，(\d+)岁）/is',$tmpData,$ageMatch);
		if(!empty($ageMatch[1])){
			$sex =$ageMatch[1];
			$resume['sex']=$sexList[$sex];
			$resume["sex_cn"]=$sex;
		}

		$resume["age"]=23;
		if(!empty($ageMatch[2])){
			$age = intval($ageMatch[2]);
			$resume["age"]=$age;
		}
		$resume["nature"]=62;
		$resume["nature_cn"]='全职';
		$resume["recentjobs"]='';
		$resume["intention_jobs"]='';
		preg_match('#工　　种</th><td width="35%">([^<]+)</td>#is',$tmpData,$jobsMatch);
		if(!empty($jobsMatch[1])){
			$resume["intention_jobs"]=$jobsMatch[1];
		}
		$resume["website"]='';
		$resume["photo"]='';
		$resume["photo_audit"]=0;
		$resume["photo_display"]=2;

		$resume["trade"]='45';
		$resume["trade_cn"]='其他行业';
		$resume["height"]=0;
		$resume["marriage"]=3;
		$resume["marriage_cn"]='保密';
		//工作年限
		preg_match('#工作年限</th><td>([^<]+)</td>#i',$tmpData,$match);
		if(!empty($match[1])){
			$exper=trim($match[1]);
			$resume["experience_cn"]=$exper;
			$resume["experience"]=$this->dbGetExperience($exper);
		}

		preg_match('#<td>\s+([\d\-]+)(&nbsp;)*元\s+</td>#is',$tmpData,$match);
		if(!empty($match[1])){
			$wagecn=$match[1];
			$wagecn=str_replace("-",'~',$wagecn);
			$wagecn=empty($wagecn) ? '' : $wagecn.'元/月';
			$wage = $this->dbGetWage($wagecn);

			$resume["wage"]=$wage;
			$resume["wage_cn"]=$wagecn;
		}

		$resume["householdaddress"]=$tmpCityCn;
		preg_match('#工作区域</th><td>([^<]+)</td>#i',$tmpData,$match);
		if(!empty($match[1])){
			$zone=$match[1];
			$resume["householdaddress"]=$tmpCityCn.$zone;
		}
		
		preg_match('#学　　历</th><td>([^<]+)</td>#i',$tmpData,$match);
		if(!empty($match[1])){
			$resume["education"]=$this->dbGetEducation($match[1]);
			$resume["education_cn"]=$match[1];
		}
		$resume["qq"]='';
		$resume["address"]=$resume["householdaddress"];

		preg_match('#介绍</th></tr>\s+<tr>\s+<td align="left" class="textline">\s+(.+?)\s+</td>#is',$tmpData,$match);
		if(!empty($match[1])){
			$resume["specialty"]=$match[1];
		}
		
		preg_match('#发布日期</th>\s+<td>([\d\-]+)</td>#is',$tmpData,$match);
		if(!empty($match[1])){
			$resume["addtime"]=strtotime($match[1]);
		}
		$resume["refreshtime"]=0;
		$resume["deadline"]=intval($resume["addtime"]*1.01);
		$resume["talent"]=3;
		$resume["complete"]=1;
		$resume["complete_percent"]=80;
		$resume["user_status"]=1;
		$resume["click"]=rand(10,178);


		$key=array();
		$len = mb_strlen($resume["intention_jobs"],"utf-8");
		for($s=0;$s<$len;$s++){
			$char=mb_substr($resume["intention_jobs"],$s,1,'utf-8');
			$key[$s]=$char;
		}
		$resume["key"]=implode('000 ',$key);

		$sql="select id from qs_resume where nameImg='".$resume["nameImg"]."' and intention_jobs='".$resume["intention_jobs"]."'";
		echo $sql;
		echo "<br />\n";
		$row = $conn->getone($sql);
		if(empty($row['id'])){
			$conn->insert($resume,'qs_resume',' insert ');
			$rid = $conn->insert_id();
		}else{
			$rid = $row['id'];
		}

		$sql="select id from qs_resume_jobs where uid=1 and pid=$rid";
		echo "<br />\n";
		$row = $conn->getone($sql);
		if($rid && empty($row['id'])){
			$cateData=array();
			$cateData["uid"]=1;
			$cateData["pid"]=$rid;

			$cateData["category"]=419;
			$cateData["subclass"]=443;
			$conn->insert($cateData , 'qs_resume_jobs');
		}
	}

	public function deleteEmptyName(){
		global $conn;

		$sql="delete from qs_resume_jobs where pid in(select id from qs_resume where nameImg='' and fullname='')";
		$conn->query($sql);
		
		$sql="delete from qs_resume where nameImg='' and fullname=''";
		$conn->query($sql);

		$sql="delete from qs_resume_jobs where pid in(select id from qs_resume where telImg='' and telephone='')";
		$conn->query($sql);

		$sql="delete from qs_resume where telImg='' and telephone=''";
		$conn->query($sql);
		echo "<br />\n 操作完成!<br />\n";
	}

	public function dbGetWage($wageName){
		global $conn;
		if(empty($wageName)){
			return 0;
		}

		$sql="select c_id,c_name from qs_category where c_alias='QS_wage' and c_name='$wageName'";
		$tradeRow = $conn->getone($sql);

		if($tradeRow){
			return ($tradeRow["c_id"]);
		}else{
			$sql="insert into qs_category (c_parentid,c_alias,c_name,c_order,stat_jobs,stat_resume) values(0,'QS_wage','$wageName',0,'','')";
			$conn->query($sql);
			$c_id = $conn->insert_id();
		}

		return ($c_id);
	}

	public function dbGetZone($zoneName,$parentid=0){
		global $conn;
		if(empty($zoneName)){
			return 0;
		}

		$sql="select id,parentid,categoryname from qs_category_district where categoryname='$zoneName'";
		$tradeRow = $conn->getone($sql);

		if($tradeRow){
			return ($tradeRow["id"]);
		}else{
			$sql="insert into qs_category_district (parentid,categoryname) values($parentid,'$zoneName')";
			$conn->query($sql);
			$c_id = $conn->insert_id();
		}

		return ($c_id);
	}

	public function dbGetExperience($experience){
		global $conn;
		if(empty($experience)){
			return 0;
		}

		$sql="select c_id,c_name from qs_category where c_alias='QS_experience' and c_name='$experience'";
		$tradeRow = $conn->getone($sql);

		if($tradeRow){
			return ($tradeRow["c_id"]);
		}else{
			$sql="insert into qs_category (c_parentid,c_alias,c_name,c_order,stat_jobs,stat_resume) values(0,'QS_experience','$experience',0,'','')";
			$conn->query($sql);
			$c_id = $conn->insert_id();
		}

		return ($c_id);
	}

	public function dbGetEducation($education){
		global $conn;
		if(empty($education)){
			return 0;
		}

		$sql="select c_id,c_name from qs_category where c_alias='QS_education' and c_name='$education'";
		$tradeRow = $conn->getone($sql);

		if($tradeRow){
			return ($tradeRow["c_id"]);
		}else{
			$sql="insert into qs_category (c_parentid,c_alias,c_name,c_order,stat_jobs,stat_resume) values(0,'QS_education','$education',0,'','')";
			$conn->query($sql);
			$c_id = $conn->insert_id();
		}

		return ($c_id);
	}

	public function storeUrlIntoFile($contents,$file){
		$fp=fopen($file,"a+");
		fwrite($fp,$contents."\n");
		fclose($fp);
	}
	
}

$caiji=new jincheng();
$caiji->goinCityPage();

$fieldArr=array('atime','pageurl');
$caiji->pickDetailInfo($fieldArr);
$caiji->deleteEmptyName();